package net.sxnic.item.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxnic.comm.CommConstant;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.action.MdataAction;
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.transition.Transition;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
public class Verify extends MdataAction {
	
	private String verifyer;
	private Task task;
	private Map<String,String> nextVerifyTrs;
	private List<Task> verifyList;
	
	@Autowired
	private UserManager userManager;
	

	public void prepare() throws Exception {
		task = taskManager.get(taskId);
	}

	public String pre(){	
		//年份处理，若属性表中未定义，则读取当前年
		String currentYear = PropertyUtil.getProperty("item.currentyear");
		if(StringUtils.isBlank(currentYear)) {
			currentYear = ItemConstants.APP_YEAR;
		}
				
		//找到当前任务节点的后续 形成单选钮
		task = taskManager.get(taskId);
		if(task.isActived()){
			optCate="edit";
			if(!task.getMdata().getAppYear().equals(this.getJhYear(currentYear))){			
				optCate="view";
			}
		}else{
			optCate="view";
		}
		
		User user = ucenterService.findUserByUserName(AppContext
				.getUserName(request));
		
		nextVerifyTrs = new LinkedHashMap<String,String>();
		
		for(Transition tr:task.getNode().getTransitions()){
			nextVerifyTrs.put(tr.getName(), tr.getName());
		}
		
		if(StringUtils.isBlank(task.getVerifier())){
			task.setVerifier(UgrConstant.USER_MAP.get(user.getUsername()));
		}
		
		if(StringUtils.isBlank(task.getTrs())){
			Object[] objs = nextVerifyTrs.keySet().toArray();
			if(objs.length>0){
				task.setTrs(String.valueOf(objs[0]));
			}			
		}
		
		if(StringUtils.isBlank(task.getComments())){
			task.setComments("同意");
		}
		
		//审核列表				
		DetachedCriteria dc = DetachedCriteria.forClass(Task.class);
		
		dc.addOrder(Order.asc("creationDate"));
		dc.add(Restrictions.eq("mdata", task.getMdata()));
		if("edit".equals(optCate)){
			dc.add(Restrictions.ne("id", task.getId()));
		}		
		//20140313孙宇飞加 根据不同系统 查询节点也不同，所以在属性表中专门定义此条件
		String[] taskNames = new String[]{"受理"};
		if(CommConstant.PROPERTY_MAP.containsKey("item.verify.taskNames")){
			taskNames = StringUtils.split(CommConstant.PROPERTY_MAP.get("item.verify.taskNames"),",");
		}
		dc.add(Restrictions.in("name", taskNames));
		dc.add(Restrictions.in("status", new String[]{Task.TASK_STATUS_ACTIVE,Task.TASK_STATUS_ENDED}));
		dc.add(Restrictions.eq("lastOne", true));
		
		verifyList = taskManager.getByDetachedCriteria(dc);		

		msg = "saveSuccess";
		
		return SUCCESS;
	}
	
	public String save() throws Exception{		
		
		try {
			if(StringUtils.isBlank(task.getDept())){
				User user =userManager.getUserByUsername(AppContext.getUserName());				
				task.setDept(user.getDept().getCode());			
			}
			
			String tempCate = optCate;
			taskManager.update(task);	
			mdata = task.getMdata();
			//为显示准备数据
			pre();
			//保存
			if("save".equals(tempCate)){			
				addActionMessage("保存成功");
				optCate="edit";			
			}
			//提交
			if("submit".equals(tempCate)){			
				try {
					signal(task, task.getTrs());
				} catch (Exception e) {
					e.printStackTrace();
				}
				addActionMessage("提交成功");
				optCate="view";	
			}
			
			//审核列表				
			DetachedCriteria dc = DetachedCriteria.forClass(Task.class);
			
			dc.addOrder(Order.asc("creationDate"));
			dc.add(Restrictions.eq("mdata", task.getMdata()));
			if("edit".equals(optCate)){
				dc.add(Restrictions.ne("id", task.getId()));
			}	
			//20140313孙宇飞加 根据不同系统 查询节点也不同，所以在属性表中专门定义此条件
			String[] taskNames = new String[]{"受理"};
			if(CommConstant.PROPERTY_MAP.containsKey("item.verify.taskNames")){
				taskNames = StringUtils.split(CommConstant.PROPERTY_MAP.get("item.verify.taskNames"),",");
			}
			dc.add(Restrictions.in("name", taskNames));
			dc.add(Restrictions.in("status", new String[]{Task.TASK_STATUS_ACTIVE,Task.TASK_STATUS_ENDED}));
			dc.add(Restrictions.eq("lastOne", true));
			
			verifyList = taskManager.getByDetachedCriteria(dc);		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String preView(){		
		pre();
		
		return SUCCESS;
	}

	public String getVerifyer() {
		return verifyer;
	}

	public void setVerifyer(String verifyer) {
		this.verifyer = verifyer;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Map<String, String> getNextVerifyTrs() {
		return nextVerifyTrs;
	}

	public void setNextVerifyTrs(Map<String, String> nextVerifyTrs) {
		this.nextVerifyTrs = nextVerifyTrs;
	}

	public List<Task> getVerifyList() {
		return verifyList;
	}

	public void setVerifyList(List<Task> verifyList) {
		this.verifyList = verifyList;
	}

	
	
}
